package com.example.demo.run;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.ExcelBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.example.demo.bean.Menutemplet;
import com.example.demo.tools.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.util.SheetBuilder;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 容器初始化完成后执行
 * EasyExcel 写操作
 */
@Component
@Order(2)
@Slf4j
public class MyRunner2 implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<Menutemplet> list = new ArrayList();

        for (int i = 0; i < 5; i++) {
            Menutemplet temp = new Menutemplet();
            temp.setId(i);
            temp.setPerm_id(i+10);
            temp.setPerm_name("哈哈哈-"+i);
            temp.setPerm_level(i+1);
            temp.setUp_perm_id(i+20);
            temp.setTime(new Date());
            list.add(temp);
        }

        String path = System.getProperty("user.dir");
        //方法一
        //设置密码
//        EasyExcel.write(path+ File.separator +"temp.xls", Menutemplet.class).password("123").sheet("模板").doWrite(list);

        //方法二
        ExcelWriter writer = null;
        try {
            writer = EasyExcel.write(path+ File.separator +"temp2.xls",Menutemplet.class).password("123").build();
            WriteSheet sheet = EasyExcel.writerSheet("模板2").build();
            writer.write(list,sheet);
        }finally {
            if(writer != null){
                writer.finish();
            }
        }
        log.debug("~~~~~~~~~~{}:写文件完成~~~~~~~~~~~~~~~","EasyExcel API");
    }
}
